// 获取product
var shop = document.querySelector(".shopping");
var name1 = document.querySelector(".name");
// 接受每个div的内容
var arr = [];

function getUrl(url) {
    var urlArr = url.split("?")[1];
    console.log(urlArr);
    urlArr = urlArr.split("&");
    console.log(urlArr);

    var obj = {};
    urlArr.forEach(item => {
        var itemArr = item.split("=");
        obj[itemArr[0]] = itemArr[1];
    });
    return obj;
}
// 商品详情页
(function () {
    var urlstyring = decodeURI(location.href);
    console.log(urlstyring);

    let regExp = /(?<=imgurl=)[^'"]*/;
    var pObj = getUrl(urlstyring);
    console.log(pObj);

    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://chst.vip:1234/api/getproduct?productid=" + pObj.productid, true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var res = JSON.parse(xhr.responseText);
            console.log(res.result);
            var rex = res.result;
            var ele = "";
            rex.forEach(item => {
                ele += `
                <div class="img1">
               ${item.productImg}
            </div>
            
                <span class="tv">${item.productName}</span>
            
                ${item.bjShop}
                <span class="comment">网友评价</span>
                <div class="name"></div>
                `
            })
            // 获取到每个div给每个div中写入内容

            shop.innerHTML = ele;
        }

    }
    $("gotop").click(function () {
        $("body,html").animate({ scrollTop: 0 }, 1000);
    })
})();

// 评论
(function () {
    var urlstyring = decodeURI(location.href);
    console.log(urlstyring);

    let regExp = /(?<=imgurl=)[^'"]*/;
    var pObj = getUrl(urlstyring);
    console.log(pObj);

    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://chst.vip:1234/api/getproductcom?productid=" + pObj.productid, true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var res = JSON.parse(xhr.responseText);
            console.log(res.result);
            var rex1 = res.result;
            var ele1 = "";
            rex1.forEach(item => {
                ele1 += `

                <div class="name">
    <span>${item.comName}</span>
    <span>${item.comTime}</span>
    <span class="context">${item.comContent}</span>
</div>

                `
            })
var name = document.querySelector(".name");


            shop.innerHTML = ele1;
        }

    }

})();
